<?php

namespace App\Model;

use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Db\Cursor;
use EasySwoole\ORM\Db\CursorInterface;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Pool\Manager;
use Throwable;

/**
 * Class UserMoneyModel
 * Create With Automatic Generator
 * @property $id
 * @property $user_id
 * @property $amount
 * @property $balance
 * @property $create_time
 * @property $update_time
 */
class UserMoneyModel extends AbstractModel
{
    protected $tableName = 'bee_user_money';

    protected $primaryKey = 'id';

    protected $autoTimeStamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * 获取用户金额
     * @param int $userId
     * @return array
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws Exception
     * @throws Throwable
     */
    public function findUserMoney(int $userId)
    {
        $keyName = 'user_money_' . $userId;
        $redis = Manager::getInstance()->get('redis')->getObj();
        if ($redis->exists($keyName)) {
            $infoArray = $redis->hValS($keyName);
        } else {
            $infoArray = $this->where('user_id', $userId)->get()->toArray();
            unset($infoArray['create_time'], $infoArray['update_time']);
            $redis->hMSet($keyName, $infoArray);
        }
        Manager::getInstance()->get('redis')->recycleObj($redis);
        return $infoArray;
    }

    /**
     * 初始化添加用户金额
     * @param int $userId
     * @return bool|int|string
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function addUserMoney(int $userId)
    {
        $res = $this->data([
            'user_id' => $userId,
            'amount' => 0,
            'balance' => 0,
            'create_time' => time(),
            'update_time' => time(),
        ], false)->save();

        if (is_numeric($res)) {
            $infoArray = $this->where('user_id', $userId)->get()->toArray();
            unset($infoArray['create_time'], $infoArray['update_time']);
            go(function () use ($res, $infoArray) {
                $redis = Manager::getInstance()->get('redis')->getObj();
                $redis->hMSet('user_money_' . $res, $infoArray);
                Manager::getInstance()->get('redis')->recycleObj($redis);
            });
        }

        return $res;
    }

    /**
     * 更新用户金额
     * @param int $userId
     * @param array $data
     * @return UserMoneyModel|array|bool|AbstractModel|Cursor|CursorInterface|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function updateUserMoney(int $userId, array $data)
    {
        if (!count($data)) {
            return false;
        }

        $info = $this->where('user_id', $userId)->get();
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $this->$k = QueryBuilder::$v[0]($v[1]);
            } else {
                $this->$k = $v;
            }
        }
        $info->update();

        go(function () use ($userId, $data) {
            $redis = Manager::getInstance()->get('redis')->getObj();
            foreach ($data as $k => $v) {
                if (is_array($v)) {
                    if (is_int($v[1])) {
                        $redis->hIncrBy('user_money_' . $userId, $k, ($v[0] == 'inc' ? $v[1] : $v[1] * -1));
                    } elseif (is_float($v[1])) {
                        $redis->hIncrByFloat('user_money_' . $userId, $k, ($v[0] == 'inc' ? $v[1] : $v[1] * -1));
                    }
                } else {
                    $redis->hSet('user_money_' . $userId, $k, $v);
                }
            }
            Manager::getInstance()->get('redis')->recycleObj($redis);
        });

        return $info->toArray();
    }
}